package com.lc.big_event.service.Impl;

import com.lc.big_event.mapper.UserMapper;
import com.lc.big_event.pojo.Result;
import com.lc.big_event.pojo.User;
import com.lc.big_event.service.UserService;
import com.lc.big_event.untils.JwtUtil;
import com.lc.big_event.untils.Md5Util;
import com.lc.big_event.untils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liuchun
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserMapper userMapper;
    public Result register(String username, String password) {
        //查询用户
        User user = userMapper.findByUserName(username);
        //进行注册
        if (user == null){
            String md5String = Md5Util.getMD5String(password);
            userMapper.register(username,md5String);
            return Result.success();
        }else {
            return Result.error("用户名已被占用");
        }
    }

    /**
     * 用户登录
     * @param username
     * @param password
     */
    public Result login(String username, String password) {
        //从数据库去查用户
        User loginUser = userMapper.findByUserName(username);
        if (loginUser == null){
            return Result.error("用户未注册!");
        }
        //这里注意==和equals的用法区别
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            //将用户的token存储到Redis
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(token,token,1, TimeUnit.HOURS);

            return Result.success(token);
        }else {
            return Result.error("密码错误!");
        }
    }

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    public User findUserByName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    /**
     * 更新用户基本信息
     * @param user
     */
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    /**
     * 更新用户头像
     * @param avatarUrl
     */
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    /**
     * 更新用户密码
     * @param map
     */
    public Result updatePwd(Map<String, String> map,String token) {
        String oldPwd = map.get("old_pwd");
        String newPwd = map.get("new_pwd");
        String rePwd = map.get("re_pwd");
        //原密码，新密码，确认新密码都不为空
        if (oldPwd == null | newPwd == null | rePwd == null ){
            return Result.error("密码有空");
        }
        //判断原密码是否正确
        Map<String,Object> claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        Integer id = (Integer) claims.get("id");
        User user = userMapper.findByUserName(username);
        if (!user.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码错误");
        }
        //判断新密码和确认新密码是否一致
        if (!rePwd.equals(newPwd)){
            return Result.error("修改的密码两次不一致");
        }
        //删除原来的token
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);

        //进行修改
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
        return Result.success();
    }
}
